package com.qtummatrix.controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.qtummatrix.entity.LoanproductsEntity;
import com.qtummatrix.service.ProductService;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("productAction")
public class ProductController extends ActionSupport implements ModelDriven<LoanproductsEntity> {



    @Autowired
    private ProductService productService;




    private Integer productId;      //删除产品时的产品ID
//    private int  productBank;
//    private String  productName;
//    private int  productSerie;
//    private int  productState;

    private LoanproductsEntity loanproductsEntity = new LoanproductsEntity();   //新建产品

//    public int getProductBank() {
//        return productBank;
//    }
//
//    public void setProductBank(int productBank) {
//        this.productBank = productBank;
//    }
//
//    public String getProductName() {
//        return productName;
//    }
//
//    public void setProductName(String productName) {
//        this.productName = productName;
//    }

//    public int getProductSerie() {
//        return productSerie;
//    }
//
//    public void setProductSerie(int productSerie) {
//        this.productSerie = productSerie;
//    }
//
//    public int getProductState() {
//        return productState;
//    }
//
//    public void setProductState(int productState) {
//        this.productState = productState;
//    }

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }



    public LoanproductsEntity getLoanproductsEntity() {
        return loanproductsEntity;
    }

    public void setLoanproductsEntity(LoanproductsEntity loanproductsEntity) {
        this.loanproductsEntity = loanproductsEntity;
    }



    private Map<String, Object> info = new HashMap<String, Object>();

    public Map<String, Object> getInfo() {
        return info;
    }


    /**
     * 获取产品列表信息
     * @return
     */
    public String getProductList() {

        int bankid = loanproductsEntity.getBankid();
        String loanName = loanproductsEntity.getLoanName();
        int seriesid = loanproductsEntity.getSeriesid();
        int state = loanproductsEntity.getState();
//        System.out.println("查询条件为："+bankid+","+loanName+","+seriesid+","+state);

        HttpServletRequest request = ServletActionContext.getRequest();
        //当前页码
        int pageNo = Integer.parseInt(request.getParameter("pageNo")) - 1;
        //页面的条数
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));//默认10条数据

        //查询产品信息
        List<Map<String, Object>> productList = productService.getProductList(bankid,loanName,seriesid,state,pageNo,pageSize);
        info.put("list", productList);

        //获取产品的记录数
        Integer productSize = productService.getProductSize(bankid,loanName,seriesid,state);
//        System.out.println("产品记录数："+productSize);

        // 返回的数据列表
        // 当前页数
        info.put("pageNum", pageNo + 1);
        // 总页数
        int pages = productSize % pageSize == 0 ? productSize / pageSize : productSize /pageSize + 1;
        info.put("pages", pages);
        // 总条数
        info.put("total", productSize);
        loanproductsEntity = new LoanproductsEntity();
        return SUCCESS;
    }


    /**
     * 获取所有所属银行和产品系列和产品状态
     * @return
     */
    public String getAllComOrgRole() {
        //获取所有所属银行
        List<Map<String, Object>> allBankName = productService.getAllproductBankName();
        //获取所有产品系列
        List<Map<String, Object>> allSerie = productService.getAllproductSerie();
        //获取所有产品状态
        List<Map<String, Object>> allState= productService.getAllproductState();
        info.put("allBankName", allBankName);//所属银行
        info.put("allSerie", allSerie);//产品系列
        info.put("allState", allState);//产品状态
        return SUCCESS;
    }

    /**
     * 删除产品列表中的产品
     * @return
     */
    public String deleteProduct(){
        int id = getProductId();
        System.out.println("删除产品,id："+id);

        int count = productService.deleteProduct(id);

        if (count == 1) {
            info.put("deleteResult", "1");
        } else {
            info.put("deleteResult", "0");
        }

        return SUCCESS;
    }


    /**
     * 修改产品状态
     * @return
     */
    public String updateProductState(){
        int id = loanproductsEntity.getId();
        int state = loanproductsEntity.getState();
        System.out.println("修改产品状态,id："+id+"状态："+state);

        int count = productService.updateProductState(id,state);
        System.out.println(count);

        if (count == 1) {
            info.put("updateResult", "1");
        } else {
            info.put("updateResult", "0");
        }
        loanproductsEntity = new LoanproductsEntity();
        return SUCCESS;
    }

    /**
     * 新建产品
     * @return
     */
    public String addProduct(){
        System.out.println("开始新建产品");
        int bankid = loanproductsEntity.getBankid();
        String loanName = loanproductsEntity.getLoanName();
        int seriesid = loanproductsEntity.getSeriesid();
        String definition = loanproductsEntity.getDefinition();
        String customer = loanproductsEntity.getCustomer();
        BigDecimal benefitsDown = loanproductsEntity.getBenefitsDown();
        BigDecimal benefitsUp = loanproductsEntity.getBenefitsUp();
        Integer shortestperiod = loanproductsEntity.getShortestperiod();
        Integer longestperiod = loanproductsEntity.getLongestperiod();
        BigDecimal rateDown = loanproductsEntity.getRateDown();
        BigDecimal rateUp = loanproductsEntity.getRateUp();
        String logo = loanproductsEntity.getLogo();

        int count = productService.addProduct(bankid,loanName,seriesid,definition,customer,benefitsDown,benefitsUp,shortestperiod,longestperiod,rateDown,rateUp,logo);
        if (count != 0) {
            info.put("addResult", "1");
        } else {
            info.put("addResult", "0");
        }
        loanproductsEntity = new LoanproductsEntity();
        return SUCCESS;
    }


    /**
     * 获取产品详细信息
     * @return
     */
    public String getProductinfo(){
        int id = loanproductsEntity.getId();
//        System.out.println("获取产品详细信息,id："+id);

        List<Map<String, Object>> list = productService.getProductinfo(id);
//        System.out.println(list);

        info.put("list",list);

        loanproductsEntity = new LoanproductsEntity();
        return SUCCESS;
    }

    /**
     * 修改产品详细信息
     * @return
     */
    public String updateProductinfo(){
        int id = loanproductsEntity.getId();
        String definition = loanproductsEntity.getDefinition();
        String customer = loanproductsEntity.getCustomer();
//        System.out.println("修改产品详细信息,id："+id+","+definition+","+customer);

        int count = productService.updateProductinfo(id,definition,customer);
//        System.out.println(count);

        if (count == 1) {
            info.put("updateResult", "1");
        } else {
            info.put("updateResult", "0");
        }
        loanproductsEntity = new LoanproductsEntity();
        return SUCCESS;
    }

	public LoanproductsEntity getModel() {
		// TODO Auto-generated method stub
		return loanproductsEntity;
	}


}